set more off
clear all

cap log close
log using $DIR\cr_enia_ip, t replace

********************************************************************************
*** CH AUG 2013/updated JAN 2016
*** merge ENIA panel with IP panel
*** modified by BHH to include pre-1995 IP data
********************************************************************************

*-------------------------- IP data --------------------------*

*** trademark data updated (previous data contained renewals)
insheet using $IPDIR\tm_ine_applnREV2.csv, clear
keep id_base year_appln tm_appln_c
rename id_base id
save $IPDIR\temp, replace

*** other IP (including old tm data)
import excel $IPDIR\matched_ip.xlsx,  firstrow clear
*** drop old tm data
drop tm*
rename idbase id
drop if  p_appln_c==. & u_appln_c==. & d_appln_c==. & db_appln_c==.
*** merge with corrected tm data
merge 1:1 id year using $IPDIR\temp, nogen
rename year_appln year
do cleanfile
save $IPDIR\ipdata, replace

*----------------- merge IP data with ENIA -------------------*

*** ENIA
use $ENIADIR\enia, clear
rename ano year
*** merge with IP data
merge 1:1 id year using $IPDIR\ipdata
tab year _m
gen keep = _m==3 | _m==1
egen x = sum(keep),by(id)
keep if x>0
drop _m x 
tab year keep
drop keep
foreach x of varlist p_appln_c u_appln_c d_appln_c db_appln_c tm_appln_c {
  replace `x'=0 if `x'==.
}

drop enia
*** Merge in constructed capital stock
merge 1:1 id year using $ENIADIR\corrected_capital, keep(1 3) nogen

label var id "A number that identifies an establishment"
label var id_region "Region"
label var id_imarval "Patent Rights to use"
label var id_evtas "Strata Sales"
label var id_evbp "Strata Gross Value of Production"
label var id_eva "Stratum Value Added"
label var id_ciiu2 "ISIC-Rev 2"
label var id_ciiu3 "ISIC-Rev 3"
label var id_tamano "Size"
label var year "Year of data and IP applns"
label var id_forpro "Form of ownership"
label var id_jortra "Normal working day"
label var id_orgjur "Type of legal organization"
label var id_diatra "Number of days worked"
label var id_diapar "Number of days not worked"
label var id_teed "Size of establishment of direct employment"
label var id_proprh "Average Owners Men"
label var id_proprm "Average Owners Women"
label var id_dirprh "Average male executives"
label var id_dirprm "Average female executives"
label var id_espprh "Average specialized/skilled male employees"
label var id_espprm "Average specialized/skilled female employees"
label var id_admprh "Average administrative male employees"
label var id_admprm "Average administrative female employees"
label var id_comprh "Average temporary male employees"
label var id_comprm "Average temporary female employees"
label var id_ndiprh "Average unskilled direct male employees"
label var id_ndiprm "Average unskilled direct female employees"
label var id_ninprh "Average unskilled indirect male employees"
label var id_ninprm "Average unskilled Indirect female employees"
label var id_serprh "Average services male employees"
label var id_serprm "Average services female employees"
label var id_tothom "Total average male employees with contract"
label var id_totmuj "Total average female employees with contract"
label var id_rememp "Payroll employees"
label var id_regemp "bonus payments employees"
label var id_itc "Contract Entry Jobs"
label var id_tocc "Total Contract Occupied average"
label var id_tohsc "Total ocupac average. Men Without Contract"
label var id_tomsc "Total ocupac average. Women Without Contract"
label var id_tosc "Average Total Occupied without Contract"
label var id_hrsprop "Hours worked in the year Owners"
label var id_hrsdirec "Hours worked in the year Directors"
label var id_hrsespec "Hours worked in the year specialized/skilled workers"
label var id_hrsadmin "Hours worked in the Administrative Pers year"
label var id_hrscoms "Hours worked in the year temporary employees"
label var id_hrsnocald "Trabajad Hrs. Trab Year. Unskilled Directive."
label var id_hrsnocali "Hrs worked. Trab Year. No rated this.Indirectly."
label var id_hrsmant "Hours worked in the year Trab. From Mantenc."
label var id_hrsaux "Auxiliary Workers Hours unskilled workers"
label var id_hrsserv "Hours worked in the year Trab. Serv.personal"
label var id_hrsespsub "Job Hrs. Trab Year.subcontracted skilled workers"
label var id_hrsadmsub "Persons Hrs Administ.subcontrat trab.en year."
label var id_remaux "Payroll unskilled workers"
label var id_regaux "bonus and other remuneration for unskilled workers"
label var id_dleaux "Job Desc.Legales.auxiliary production"
label var id_cfamaux "Job Family Burdens.auxiliary production"
label var id_thhano "Total Man Hours Year"
label var id_thhdir "Total Man Hours Direct Employees"
label var id_thhind "Indirect Employees Total Man Hours"
label var id_hhdiadir "HH Day Average Direct Employees"
label var id_hhdiaind "Indirect Employees HH Average Day"
label var id_hhdiapr "Total Average HH Day"
label var id_mtmpval "Total raw materials"
label var id_mimpval "Imported raw materials"
label var id_mrevval "Items purchased for resale"
label var id_mconval "Cost of work done under contract"
label var id_msubval "Cost of work performed by subcontract"
label var id_tagcan "Total Amount water"
label var id_tagval "Total Net water"
label var id_comcan "Quantity purchased electricity"
label var id_comval "Net electricity purchased"
label var id_gencan "electricity quantity generated"
label var id_vencan "electricity quantity sold"
label var id_venval "Net value of electricity sold"
label var id_tcoval "Total net worth of fuel"
label var id_tseval "Total service expenses"
label var id_edidep "Depreciation buildings"
label var id_maqdep "Depreciation machinery"
label var id_muedep "Depreciation furniture, useful, facilities and other assets"
label var id_vehdep "Vehicle Depreciation"
label var id_otrdep "Depreciation other assets"
label var id_diegas "Diets and directory shares"
label var id_intgas "Interests"
label var id_divgas "Dividends"
label var id_totiva "Total taxes paid"
label var id_divret "Dividends"
label var id_cbnter "Buying real new ground"
label var id_cbnedi "Buying real new buildings"
label var id_cbnmaq "Purchase new machinery goods, etc.."
label var id_cbnveh "Purchase new goods vehicles"
label var id_cbuter "Purchase of land used goods"
label var id_cbuedi "Buying used goods buildings"
label var id_cbumaq "Buying used goods machinery, etc.."
label var id_cbuveh "Buying used goods vehicles"
label var id_vbuter "Sale of used land"
label var id_vbuedi "Sale of used buildings"
label var id_vbumaq "Sale of used machinery, etc.."
label var id_vbuveh "Sale of used vehicles"
label var id_refter "Refor. and land improvements made ??by others"
label var id_refedi "Refor. and improvements made ??by others buildings"
label var id_refmaq "Refor. and improvements made ??by third maquin., etc.."
label var id_refveh "Refor. and improvements made ??by third vehicle"
label var id_vbnter "Value of the new finished goods land"
label var id_vbnedi "Value of the new finished goods buildings"
label var id_vbnmaq "Value of the new finished goods maquin., Etc.."
label var id_vbnveh "Value of the new finished goods vehicles"
label var id_vocter "Value of building ongoing land"
label var id_vocedi "Value of building work in progress"
label var id_vocmaq "Value of works in progress machinery, etc.."
label var id_vocveh "Value of works in progress vehicle"
label var id_corter "Correction terrain monetary asset"
label var id_coredi "Correction monetary asset building"
label var id_cormaq "Monetary asset Correction machinery, etc.."
label var id_corveh "Correction car monetary asset"
label var id_salter "Fixed asset Balance at end of period ground"
label var id_saledi "Fixed asset at end of period building"
label var id_salmaq "Fixed asset at end of period machinery, etc.."
label var id_salveh "Fixed asset at end of period vehicles"
label var id_texvap "Total initial existence"
label var id_texvaf "Total final existence"
label var id_acthab "Asset accounts"
label var id_pasdeb "Liabilities Accounts"
label var id_gashab "Spending Accounts"
label var id_ingdeb "Revenue accounts"
label var id_saldeb "Ending balance (should)"
label var id_salhab "Ending balance (have)"
label var id_costot "Total Cost of Goods Received and Contract Work performed by"
label var id_rempag "Remuneration Paid"
label var id_matpco "Raw Materials Purchased"
label var id_mateco "Purchased Materials"
label var id_coagin "Industrial Water Consumption"
label var id_elecomp "Electricity Purchased"
label var id_elecons "Electricity Consumed (thousand kWh)"
label var id_tcomco "Total Purchase Fuel"
label var id_tgasete "Total Expenses for Third Party Services"
label var id_tdeprec "Total Depreciation"
label var id_tgasfin "Total Expense"
label var id_vstk "Nominal Value of Fixed Capital Stock"
label var id_fabval "Sales of products manufactured"
label var id_expval "Value export products"
label var id_revval "Resale products"
label var id_ipatval "Direct sales of patents, licenses and trademarks"
label var id_ingtot "Total Revenues by Product Sales and Work performed"
label var id_vbp "Gross Value of Production"
label var id_va "Value Added"
label var id_abaf "Gross Addition to Fixed Assets"
label var id_vpf "Sales of Products Manufactured"
label var id_ingtotal "Total Revenues by Own Production"
label var id_ingexp "Revenue from Exports"
label var id_sharenational "Share National Capital"
label var id_shareforeign "Share Foreign Capital"

label var d_appln_c "Design (deseno) applications "
label var db_appln_c "Design (dibujo) applications"
label var tm_appln_c "Trademark applications"
label var p_appln_c "Patent applications "
label var u_appln_c "Utility model applications "
 
  
*--- matched data --------*
do $DIR\cleanfile
save $IPDIR\enia_ip, replace
*-------------------------*

log close
